<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\AdvPgmListAdmin;
use app\common\facade\model\AdvseqModel;

class Advpgmlist extends BaseController
{

    // 节目单日期单素材详情
    public function advpgmgroupList()
    {
        $param = $this->request->get([
            'type',
            'advseq',
            'ordername' => $this->request->get('ordername')?:'play_order',
            'order' => $this->request->get('order')?:'desc',
        ]);

        try {
            $data['list'] = AdvPgmListAdmin::advpgmgroupList($param);
            $total_time = 0;
            foreach ($data['list'] as $key => $val) {
                $data['list'][$key]['news'] = '';
                $total_time += $val['pgm_len'];
            }
            if (!empty($param['advseq'])) {
                $seq_info = AdvseqModel::info(['adv_seq_id'=>$param['advseq']]);
                $data['title'] = $seq_info['adv_seg_name'];
                $data['time'] = calculat_time($seq_info['s_time']);
                $data['total_time'] = calculat_time($total_time);
            }

        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    // 节目单日期单添加素材
    public function advpgmgroupAdd()
    {
        $param = $this->request->post([
            'pl_id'
        ]);

        try {
            $data = AdvPgmListAdmin::advpgmgroupNewAdd($param);
        } catch (ValidateException $e) {
            return validateJson([], $e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        if ($data['code'] == 0) {
            return validateJson([], $data['msg']);
        }
        return successJson($data);
    }

    // 节目单日期单删除素材
    public function advpgmgroupDel()
    {
        $param = $this->request->post([
            'id'
        ]);

        try {
            $data = AdvPgmListAdmin::advpgmgroupDel($param['id']);
        } catch (ValidateException $e) {
            return validateJson([], $e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    // 节目单日期单复制素材
    public function copyadvpgm()
    {
        $param = $this->request->post([
            'id'
        ]);

        try {
            $data = AdvPgmListAdmin::copyadvpgms($param);
        } catch (ValidateException $e) {
            return validateJson([], $e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    // 节目单日期单素材统计
    public function totaladvpgm()
    {
        $param = $this->request->post([
            'id'
        ]);

        try {
            $data = AdvPgmListAdmin::totaladvpgms($param);
        } catch (ValidateException $e) {
            return validateJson([], $e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    // 节目单日期单素材统计
    public function delmoreadvpgm()
    {
        $param = $this->request->post([
            'id'
        ]);

        try {
            $data = AdvPgmListAdmin::delmoreadvpgms($param);
        } catch (ValidateException $e) {
            return validateJson([], $e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    // 节目时段列表
    public function channelgroup()
    {
        $param = $this->request->get([
            'type'  => $this->request->get('type')?:null,
        ]);

        try {
            $data = AdvPgmListAdmin::channelgroup($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    // 添加节目日期单
    public function channelgroupAdd()
    {
        $param = $this->request->post([
            'channel_id',
            'play_date',
            'other_date',
            'advseqs'
        ]);

        try {
            // $this->validate($param, 'app\common\validate\Material.scenecopysave');
            $data = AdvPgmListAdmin::channelgroupAddSave($param);
        } catch (ValidateException $e) {
            return validateJson([], $e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        if ($data['code'] == 400) {
            return validateJson([], $data['msg']);
        }
        return successJson($data['data']);
    }

    // 修改广告节目单
    public function channelgroupEdit()
    {
        $param = $this->request->put([
            'channel_id',
            'pl_id',
            'play_date',
            'up_time'
        ]);

        try {
            // $this->validate($param, 'app\common\validate\Material.scenecopysave');
            $data = AdvPgmListAdmin::channelgroupAddEditSave($param);
        } catch (ValidateException $e) {
            return validateJson([], $e->getError());
        } catch(\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        return successJson($data);
    }

    // 删除广告节目单
    public function channelgroupDel()
    {
        $ids = $this->request->delete();
        $id_str = implode(',', $ids);
        try {
            $data = AdvPgmListAdmin::channelgroupDel($id_str);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }
        if ($data['code'] == 0) {
            return validateJson([], $data['msg']);
        }
        return successJson($data);
    }

    // 获取频道的节目时段列表
    public function getAdvSeqList() {
        $param = $this->request->get([
            'channel_id' => $this->request->get('channel'),
            'play_date' => $this->request->get('play_date')
        ]);

        try {
            $data = AdvPgmListAdmin::getadvseqList($param);
            // 遍历数组根据adv_seq_id判断一下该频道，该日期，该时段内是否有节目
            $total_time = 0;
            foreach ($data['list'] as $k => $val) {
                if (!empty($val['adv_seq_id'])) {
                    $info = AdvPgmListAdmin::getadvseqDetail(['channel_id'=>$param['channel_id'],'play_date'=>$param['play_date'],'advseg_id'=>$val['adv_seq_id']]);
                    if (empty($info)) {
                        $data['list'][$k]['a_class'] = 'empty_type';
                    }else{
                        $data['list'][$k]['a_class'] = 'notempty_type';
                    }
                }
            }
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    // 审核/反审广告单日期单状态
    public function reviewadvseq()
    {
        $param = $this->request->post([
            'pl_id' => $this->request->post('pl_id'),
            'word' => $this->request->post('review_word'),
            'status' => $this->request->post('review_status')
        ]);

        try {
            $data = AdvPgmListAdmin::reviewadvseq($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    // 获取频道的广告单列表
    public function getotheradvlist() {
        $param = $this->request->post([
            'channel_id' => $this->request->post('channel_id')
        ]);

        try {
            $data = AdvPgmListAdmin::getotheradvlist($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    // 根据节目日期单pl_id获取节目单时段
    public function getotheradvseq() {
        $param = $this->request->post([
            'pl_id' => $this->request->post('pl_id')
        ]);

        try {
            $data = AdvPgmListAdmin::getotheradvseq($param);
        } catch (\Exception $e)
        {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }
}